package com.delu.controller;


import com.alibaba.fastjson.JSONObject;
import com.delu.dto.LeaseDTO;

import com.delu.data.Result;
import com.delu.em.ResultEnum;
import com.delu.entity.Lease;
import com.delu.service.LeaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 租赁方式 前端控制器
 * </p>
 *
 * @author author
 * @since 2021-12-18
 */
@Controller
@RequestMapping("/lease")
@RestController
@Api(value = "租车类型接口")
public class LeaseController {

    @Autowired
    private LeaseService leaseService;

    @GetMapping("leaseList")
    @ApiOperation(value = "租车方式无参全部查询")
    public Result<List<LeaseDTO>> leaseList(){
        List<Lease> leases = leaseService.leaseList();
        String s = JSONObject.toJSONString(leases);
        List<LeaseDTO> leaseDTOS = JSONObject.parseArray(s, LeaseDTO.class);
        return Result.response(ResultEnum.SUCCESS).setData(leaseDTOS);
    }


    @PostMapping
    @ApiOperation(value = "租车类型增加")
    @ApiImplicitParam(name = "lease",value = "租车类型实体类",required = true,paramType = "body")
    public Result addlease(@RequestBody Lease lease){
        Integer integer = leaseService.addlease(lease);
        return Result.response(ResultEnum.SUCCESS).setData(integer);
    }

    @GetMapping
    @ApiOperation(value = "租车类型查询有参")
    @ApiImplicitParam(name = "leaseId",value = "基本数据类型",required = true,paramType = "query")
    public Result selectId(@RequestParam("leaseId") Integer leaseId){

        Lease lease = leaseService.selectById(leaseId);
       LeaseDTO leaseDTO = new LeaseDTO();
        BeanUtils.copyProperties(lease,leaseDTO);
        return Result.response(ResultEnum.SUCCESS).setData(leaseDTO);
    }

    @PutMapping
    @ApiOperation(value = "租车类型修改")
    @ApiImplicitParam(name = "lease",value = "租车类型实体类",required = true,paramType = "body")
    public Result updataLease(@RequestBody Lease lease){
        Integer integer = leaseService.updataLease(lease);
        return Result.response(ResultEnum.SUCCESS).setData(integer);
    }

    @DeleteMapping
    @ApiOperation(value = "租车删除")
    @ApiImplicitParam(name = "leaseId",value = "基本数据类型",required = true,paramType = "query")
    public Result deleteId(@RequestParam("leaseId") Integer leaseId){
        Integer integer = leaseService.deleteId(leaseId);
        return Result.response(ResultEnum.SUCCESS).setData(integer);
    }



}
